/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 13:44:19  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;

import com.google.common.base.Strings;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.bp.bean.BpEmergencyMaterialEditBean;
import com.joysuch.wwyt.bp.bean.BpEmergencyMaterialListBean;
import static com.joysuch.wwyt.bp.bean.BpEmergencyMaterialListBean.Search;
import com.joysuch.wwyt.bp.entity.BpEmergencyMaterial;
import com.joysuch.wwyt.bp.service.BpEmergencyMaterialService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 应急消防管理——物资管理
 */
@RestController
@Api(tags = "应急消防管理—物资管理")
@RequestMapping("/wwyt/bp-emergency-material")
public class BpEmergencyMaterialController {

    private static final Logger logger = LoggerFactory.getLogger(BpEmergencyMaterialController.class);

    @Autowired
    private BpEmergencyMaterialService bpEmergencyMaterialService;

    /**
     * 添加物资
     * @param entity    待添加的物资相关信息
     */
    @RequestMapping("add")
    public Object add(@Valid @RequestBody BpEmergencyMaterialEditBean entity) {
        if (Strings.isNullOrEmpty(entity.getName())) {
            return ResultBean.wrapErrorResultDefault("物资名称不能为空");
        }
        if (entity.getMaterialNumber() == null) {
            return ResultBean.wrapErrorResultDefault("物资数量不能为空");
        }

        try {
            bpEmergencyMaterialService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    /**
     * 修改物资
     * @param entity    待修改的物资相关信息
     */
    @RequestMapping("update")
    public Object update(@Valid @RequestBody BpEmergencyMaterialEditBean entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpEmergencyMaterialService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#"+entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    /**
     * 获取物资列表（分页）
     * @param page  分页信息
     * @param condition 筛选条件
     */
    @RequestMapping("page")
    public Object page(Pageable page, Search condition) {
        try {
            Page<BpEmergencyMaterialListBean> data = bpEmergencyMaterialService.findByPage(page,condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    /**
     * 删除物质信息
     * @param id    待删除的物质信息 ID
     */
    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpEmergencyMaterialService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }
    
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field="id") Long id) {
        try {
            return ResultBean.success(bpEmergencyMaterialService.getById(id));
        } catch (Exception e) {
            logger.error("查询详情失败", e);
            return ResultBean.fail(101, "查询详情失败:"+e.getMessage());
        }
    }
}
